import Player from './player'
import flvPlugin from './plugins/flv'
import hlsPlugin from './plugins/hls'
import dashPlugin from './plugins/dash'
import timer from './utils/timer'

let index = 0; // eslint-disable-line
const instances = {}

function viil (containerId) {
  const container = document.getElementById(containerId)
  if (!container) {
    throw new Error(
      `viil(container): container should be an id of a div in the body, but not found`
    )
  }

  if (instances[containerId]) {
    return instances[containerId]
  }

  index++
  const player = new Player(containerId)
  instances[containerId] = player
  return player
}

viil.setDefaultOptions = Player.setDefaultOptions
viil.registerPlugin = Player.registerPlugin

viil.registerPlugin('flvjs', flvPlugin)
viil.registerPlugin('hlsjs', hlsPlugin)
viil.registerPlugin('dashjs', dashPlugin)
viil.utils = {
  timer
}

export default viil
